#include "Dri_mqtt.h"

#define CONFIG_BROKER_URI "ws://broker.emqx.io:8083/mqtt"

static const char *TAG = "mqttws";

void Dri_mqtt_HandleData(esp_mqtt_event_handle_t event);

/**
 * @brief mqtt事件处理函数
 */
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id);
    esp_mqtt_event_handle_t event = event_data;
    esp_mqtt_client_handle_t client = event->client;
    int msg_id;
    switch ((esp_mqtt_event_id_t)event_id)
    {
    case MQTT_EVENT_CONNECTED:
        ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
        msg_id = esp_mqtt_client_subscribe(client, "zrc/cmd", 0);//订阅单个主题
        break;
    case MQTT_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
        break;
    case MQTT_EVENT_SUBSCRIBED:
        ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_UNSUBSCRIBED:
        ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_PUBLISHED:
        ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_DATA:
        // ESP_LOGI(TAG, "MQTT_EVENT_DATA");
        // MY_LOGE("TOPIC=%.*s\r\n", event->topic_len, event->topic);
        // MY_LOGE("DATA=%.*s\r\n", event->data_len, event->data);
        Dri_mqtt_HandleData(event);
        break;
    case MQTT_EVENT_ERROR:
        ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
        break;
    default:
        ESP_LOGI(TAG, "Other event id:%d", event->event_id);
        break;
    }
}

/**
 * @brief mqtt初始化函数
 */
void Dri_mqtt_Init(void)
{
    const esp_mqtt_client_config_t mqtt_cfg = {
        .broker.address.uri = CONFIG_BROKER_URI,
    };

    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
    /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
    esp_mqtt_client_start(client);
}

extern TaskHandle_t mainHandle;
/**
 * @brief  mqtt数据处理函数
 * @param  event:mqtt事件
 */
void Dri_mqtt_HandleData(esp_mqtt_event_handle_t event)
{
    char topic[30] = {0};
    memcpy(topic, event->topic, event->topic_len);
    if (strcmp(topic, "zrc/cmd") != 0)
        return;
    char data[100] = {0};
    memcpy(data, event->data, event->data_len);
    cJSON *data_json = cJSON_Parse(data);
    cJSON *cmdItem = cJSON_GetObjectItem(data_json, "cmd");
    cJSON *typeItem = cJSON_GetObjectItem(data_json, "type");
    cJSON *dirItem = cJSON_GetObjectItem(data_json, "dir");
    char *cmd = cJSON_GetStringValue(cmdItem);
    char *type = cJSON_GetStringValue(typeItem);
    char *dir = cJSON_GetStringValue(dirItem);

    MY_LOGE("cmd:%s,type:%s,dir:%s", cmd, type, dir);
    if (strcmp(cmd, "on") == 0)
    {
        if (strcmp(type, "audio") == 0)
        {
            if (strcmp(dir, "esp2client") == 0)
            {
                xTaskNotify(mainHandle, AUDIO_ON_ESP2CLIENT, eSetValueWithOverwrite);
            }
            else if (strcmp(dir, "client2esp") == 0)
            {
                xTaskNotify(mainHandle, AUDIO_ON_CLIENT2ESP, eSetValueWithOverwrite);
            }
        }
        else if (strcmp(type, "video") == 0)
        {
            if (strcmp(dir, "esp2client") == 0)
            {
                xTaskNotify(mainHandle, VIDEO_ON_ESP2CLIENT, eSetValueWithOverwrite);
            }
        }
    }
    else if (strcmp(cmd, "off") == 0)
    {
        if (strcmp(type, "audio") == 0)
        {
            if (strcmp(dir, "esp2client") == 0)
            {
                xTaskNotify(mainHandle, AUDIO_OFF_ESP2CLIENT, eSetValueWithOverwrite);
            }
            else if (strcmp(dir, "client2esp") == 0)
            {
                xTaskNotify(mainHandle, AUDIO_OFF_CLIENT2ESP, eSetValueWithOverwrite);
            }
        }
        else if (strcmp(type, "video") == 0)
        {
            if (strcmp(dir, "esp2client") == 0)
            {
                xTaskNotify(mainHandle, VIDEO_OFF_ESP2CLIENT, eSetValueWithOverwrite);
            }
        }
    }
    cJSON_Delete(data_json);
}
